import requests
from requests.auth import HTTPBasicAuth

# 设置 RabbitMQ 管理 API URL 和凭证
url = "http://10.5.1.48:15672/api/queues"
username = "cloud"
password = "7#BD2CFc3TBV}7m7z"

# 发送 HTTP GET 请求
response = requests.get(url, auth=HTTPBasicAuth(username, password))

# 检查请求是否成功
if response.status_code == 200:
    queues = response.json()

    # 初始化速率的总计
    total_deliver_manual_ack_rate = 0

    # 遍历每个队列，汇总消息速率
    for queue in queues:
        message_stats = queue.get("message_stats", {})

        # 提取各类消息速率
        deliver_auto_ack_rate = message_stats.get("deliver_no_ack_details", {}).get(
            "rate", 0
        )
        deliver_rate = message_stats.get("deliver_details", {}).get("rate", 0)

        # 计算 Deliver (manual ack) 速率
        deliver_manual_ack_rate = deliver_rate - deliver_auto_ack_rate
        total_deliver_manual_ack_rate += deliver_manual_ack_rate

    # 将结果精确到小数点后一位
    total_deliver_manual_ack_rate = round(total_deliver_manual_ack_rate, 1)

    print(f"Deliver (manual ack): {total_deliver_manual_ack_rate}/s")
else:
    print(f"数据获取错误: {response.status_code}")
